class CallForWorkersController < AdminSupportController
  resources_controller_for :call_for_workers

  before_filter :find_events, :only => [:create, :new]

  def print
    
  end

  def create
    @cfw = CallForWorker.new(params[:cfw])
    @cfw.user = current_user
    if @cfw.save and @cfw.event_ids = params[:events]
      if params[:really_send_emails] == 'true'
        User.find(:all, :conditions => ['id != ?', current_user.id]).each do |u|
          begin
            EventMailer.deliver_cfw(@cfw.events, u, @cfw.message, request.host_with_port)
            @cfw.record_delivery_success(u)
          rescue Exception => e
            @cfw.record_delivery_error(u, e.message)
            logger.error e
          end
        end
      end
      redirect_to act_events_url(:events => params[:events], :confirm => true, :state => 'cfw')
    else
      render params.merge(:action=>:new)
    end
  end
  
  def find_events
    @events = Event.find(:all, :conditions => ["state='new' and id in (?)", params[:events]])
  end
end
